const db = require('../config/config')
module.exports = {
  //获取场景全部信息
  getScenario(req, res) {
    db.dbMysql(`SELECT * FROM scenario`,
      [], function (err, data) {
        for (let i = 0; i < data.length; i++) {
          if (data[i].SceneShelf == 0) {
            data[i].SceneShelf = true;
          } else {
            data[i].SceneShelf = false;
          }
        }
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: '场景信息查询成功'
          })
        } else {
          res.send({
            code: 500,
            msg: '服务器错误'
          })
        }
      })
  },
  // 搜索页面全部信息
  getSearc(req, res) {
    db.dbMysql(`SELECT * FROM searchinfo`,
      [], function (err, data) {
        for (let i = 0; i < data.length; i++) {
          if (data[i].serShelf == 0) {
            data[i].serShelf = true;
          } else {
            data[i].serShelf = false;
          }
        }
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: '场景信息查询成功'
          })
        } else {
          res.send({
            code: 500,
            msg: '服务器错误'
          })
        }
      })
  },
  // 分类全部信息
  getcom(req, res) {
    db.dbMysql(`SELECT * FROM classify`,
      [], function (err, data) {

        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: '分类信息查询成功'
          })
        } else {
          res.send({
            code: 500,
            msg: '服务器错误'
          })
        }
      })
  },
   // 财务全部信息
   getprin(req, res) {
    db.dbMysql(`SELECT * FROM orderinfo`,
      [], function (err, data) {

        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: '分类信息查询成功'
          })
        } else {
          res.send({
            code: 500,
            msg: '服务器错误'
          })
        }
      })
  },
  // 场景删除
  delScenario(req, res) {
    db.dbMysql(` DELETE FROM scenario where SceneId=?`, [req.body.SceneId], function (err, data) {
      if (data) {
        console.log('data', data);
        res.send({
          code: 200,
          data: data,
          msg: '删除成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    })
  },
  // 搜索删除
  delSearchinfo(req, res) {
    db.dbMysql(` DELETE FROM searchinfo where seriaId=?`, [req.body.seriaId], function (err, data) {
      if (data) {
        console.log('data', data);
        res.send({
          code: 200,
          data: data,
          msg: '删除成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    })
  },
  // 查询
  getSceneId(req, res) {
    console.log(req.query.zhuangtai, '666666666666666');


    if (req.query.id == "undefined" && req.query.zhuangtai == "undefined") {
      query = "SELECT * FROM scenario";
      params = [];
    } else if (req.query.id == "undefined" && req.query.zhuangtai != "undefined") {
      query = "SELECT * FROM scenario where SceneShelf = ?";
      params = [req.query.zhuangtai];
    } else if (req.query.id != "undefined" && req.query.zhuangtai == "undefined") {
      query = "SELECT * FROM scenario where SceneNanme like ?";
      params = [`%${req.query.id}%`];
    } else if (req.query.id != "undefined" && req.query.zhuangtai != "undefined") {
      query =
        "SELECT * FROM scenario where SceneNanme like ? AND SceneShelf = ?";
      params = [`%${req.query.id}%`, req.query.zhuangtai];
    }

    db.dbMysql(query, params,
      function (err, data) {
        for (let i = 0; i < data.length; i++) {
          if (data[i].SceneShelf == 0) {
            data[i].SceneShelf = true;
          } else {
            data[i].SceneShelf = false;
          }
        }
        console.log(data, '查询的');
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "查询成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "查询失败",
          });
        }
      }
    );
  },
  // 场景添加
  addTo(req, res) {
    console.log(req.body);

    const dateStr = req.body.SceneTime;
    const date = new Date(dateStr);
    date.setDate(date.getDate() + 1);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate() - 1).padStart(2, "0");
    const formattedDate = `${year}-${month}-${day}`; // "2023-04-10"

    db.dbMysql(
      `INSERT INTO scenario(SceneId,SceneNanme,SceneImg,SceneShelf,SceneTime,SceneSl) VALUES (?,?,?,?,?,?)`,
      [null, req.body.SceneNanme, req.body.SceneImg, req.body.SceneShelf, formattedDate, req.body.SceneSl],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "添加成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "添加失败",
          });
        }
      }
    );
  },
  // 搜索添加
  addSearc(req, res) {
    console.log(req.body.n2);


    db.dbMysql(
      `INSERT INTO searchinfo(seriaId,serKeywords,serShelf) VALUES (?,?,?)`,
      [null, req.body.n, req.body.n2],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "添加成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "添加失败",
          });
        }
      }
    );
  },
  // 场景编辑
  editable(req, res) {
    console.log(req.body.row, "------------------------");
    let SceneNanme = req.body.row.SceneNanme;
    let SceneTime = req.body.row.SceneTime;
    let SceneSl = req.body.row.SceneSl;
    let date = new Date(new Date(SceneTime).getTime() + 8 * 3600 * 1000);
    date = date.toJSON();
    date = date.substring(0, 10);
    console.log(date);
    if (req.body.row.SceneShelf == true) {
      req.body.row.SceneShelf = 0;
    } else {
      req.body.row.SceneShelf = 1;
    }
    let SceneId = req.body.row.dataID;
    db.dbMysql(
      `UPDATE scenario SET SceneNanme=?, SceneSl=?,  SceneTime=?, SceneShelf=? WHERE SceneId=?`,
      [SceneNanme, SceneSl, date, req.body.row.SceneShelf, SceneId],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "修改成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "修改失败",
          });
        }
      }
    );
  },
  // 搜索编辑
  editsearc(req, res) {
    console.log(req.body.row, "------------------------");
    let serKeywords = req.body.row.serKeywords;
    if (req.body.row.serShelf == true) {
      req.body.row.serShelf = 0;
    } else {
      req.body.row.serShelf = 1;
    }
    let seriaId = req.body.row.dataID;
    // console.log(req.body.row.serShelf);
    db.dbMysql(
      `UPDATE searchinfo SET serKeywords=?, serShelf=? WHERE seriaId=?`,
      [serKeywords, req.body.row.serShelf, seriaId],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "修改成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "修改失败",
          });
        }
      }
    );
  },
}